package org.jhh.domain.strategy.service.rule.tree.impl;


import lombok.extern.slf4j.Slf4j;
import org.jhh.domain.strategy.model.valobj.RuleLogicCheckTypeVO;
import org.jhh.domain.strategy.model.valobj.StrategyAwardStockKeyVO;
import org.jhh.domain.strategy.repository.IStrategyRepository;
import org.jhh.domain.strategy.service.armory.IStrategyDispatch;
import org.jhh.domain.strategy.service.rule.tree.ILogicTreeNode;
import org.jhh.domain.strategy.service.rule.tree.factory.DefaultTreeFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author Fuzhengwei bugstack.cn @小傅哥
 * @description 库存扣减节点
 * @create 2024-01-27 11:25
 */
@Slf4j
@Component("rule_stock")
public class RuleStockLogicTreeNode implements ILogicTreeNode {

    @Resource
    private IStrategyDispatch strategyDispatch;


    @Resource
    private IStrategyRepository strategyRepository;

    //    这个节点的作用是用来扣减库存的 对于库存的扣减 要使用消息队列
    @Override
    public DefaultTreeFactory.TreeActionEntity logic(String userId, Long strategyId, Integer awardId, String ruleValue, Date endDateTime) {

        log.info("规则过滤-库存扣减 userId:{} strategyId:{} awardId:{}", userId, strategyId, awardId);

        Boolean status = strategyDispatch.subtractionAwardStock(strategyId, awardId, endDateTime);
//        扣减库存
        if(status){

            log.info("规则过滤-库存扣减-成功 userId:{} strategyId:{} awardId:{}", userId, strategyId, awardId);
//            写入延迟队列 延迟消费更新数据库记录
            strategyRepository.awardStockConsumeSendQueue(StrategyAwardStockKeyVO.builder()
                    .strategyId(strategyId)
                    .awardId(awardId)
                    .build());

            return DefaultTreeFactory.TreeActionEntity.builder()
                    .ruleLogicCheckType(RuleLogicCheckTypeVO.TAKE_OVER)
                    .strategyAwardVO(DefaultTreeFactory.StrategyAwardVO.builder()
                            .awardId(awardId)
                            .awardRuleValue(ruleValue)
                            .build())
                    .build();
        }
//        异步更新数据库

                // 如果库存不足，则直接返回放行
                log.warn("规则过滤-库存扣减-告警，库存不足。userId:{} strategyId:{} awardId:{}", userId, strategyId, awardId);
                return DefaultTreeFactory.TreeActionEntity.builder()
                        .ruleLogicCheckType(RuleLogicCheckTypeVO.TAKE_OVER)
                        .build();
    }

}
